package com.css.fxfzaqbz.modules.assessplot.repository.impl;

import com.css.fxfzaqbz.constants.FxfzConstants;
import com.css.fxfzaqbz.modules.assessplot.entity.EvaluationResults;
import com.css.fxfzaqbz.modules.assessplot.repository.EvaluationResultsRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class EvaluationResultsRepositoryImpl implements EvaluationResultsRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(EvaluationResults evaluationResults) {
        String sql = "insert into sys_evaluation_results values(:id,:number,:people,:iaId,:delFlag,:createUser,:createTime,:result,:isShow)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(evaluationResults);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<EvaluationResults> getAll(EvaluationResults evaluationResults) {
        StringBuilder sql = new StringBuilder("select * from sys_evaluation_results");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0' ");
        if (!PlatformObjectUtils.isEmpty(evaluationResults.getIaId())) {
            whereSql.append(" and ia_id = '" + evaluationResults.getIaId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(evaluationResults.getResult())) {
            whereSql.append(" and result = '" + evaluationResults.getResult() + "'");
        }
        List<EvaluationResults> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EvaluationResults.class));
        return list;
    }

    @Override
    public List<EvaluationResults> getOne(String id) {
        String sql = "select * from sys_evaluation_results where del_flag ='0' and id ='" + id + "'";
        List<EvaluationResults> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EvaluationResults.class));
        return list;
    }

    @Override
    public List<EvaluationResults> getById(String id) {
        String sql = "select * from sys_evaluation_results where  id ='" + id + "'";
        List<EvaluationResults> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EvaluationResults.class));
        return list;
    }

    @Override
    public void delete(String id) {
        String sql = "DELETE FROM sys_evaluation_results where id ='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateStatus1(EvaluationResults evaluationResults) {
        String sql = " UPDATE sys_evaluation_results SET is_show ='" + evaluationResults.getIsShow() + "' WHERE id= '" + evaluationResults.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateStatus2(EvaluationResults evaluationResults) {
        String sql = " UPDATE sys_evaluation_results SET is_show ='0' where result = '" + evaluationResults.getResult() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public String getResult(String iaId) {
        String sql = "select * from sys_evaluation_results where del_flag ='0' and iaId ='" + iaId + "' and result = '1' and is_show = '1'";
        List<EvaluationResults> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EvaluationResults.class));
        if (list.size() == 0) return null;
        return list.get(0).getId();
    }
}
